//
//  ZYHud.swift
//  ZYUICommon
//
//  Created by Yi Zhou on 2025/1/13.
//

import Foundation
import UIKit
import MBProgressHUD

//MARK: - 吐司
@MainActor
public class ZYToast: NSObject {
    
    private override init() {}
    
    public static let shared: ZYToast = ZYToast()
    
    /// 文本颜色
    public var textColor: UIColor = .black
    
    /// 文本字体
    public var textFont: UIFont = UIFont.systemFont(ofSize: 16)
    
    /// 背景颜色
    public var bgColor: UIColor = ZYUtil.colorWithHex("#FFF5E0")
    
    /// 背景圆角
    public var cornerRadius: CGFloat = 8
    
    private var zyHud: MBProgressHUD?
    
    
    /// 吐司，默认1.5s后自动关闭
    /// - Parameters:
    ///   - text: text description
    ///   - hideDelay: hideDelay description
    public func show(_ text: String, hideDelay: TimeInterval = 1.5) {
        if let keyWindown = zKeyWindow {
            if zyHud != nil {
                zyHud = nil
            }
            zyHud = MBProgressHUD.showAdded(to: keyWindown, animated: true)
            zyHud?.mode = .text
            zyHud?.label.text = text
            zyHud?.label.textColor = textColor
            zyHud?.label.font = textFont
            zyHud?.bezelView.color = bgColor
            zyHud?.bezelView.layer.cornerRadius = cornerRadius
            zyHud?.isUserInteractionEnabled = false
            zyHud?.hide(animated: true, afterDelay: hideDelay)
        }
    }
}

//MARK: - 进度条
@MainActor
public class ZYProgress: NSObject {
    
    private override init() {}
    
    public static let shared: ZYProgress = ZYProgress()
    
    /// 进度条
    public var progress: CGFloat = 0 {
        didSet {
            if let hud = zyHud {
                hud.progress = Float(progress)
            }
        }
    }
    
    /// 样式
    public var mode: ZYEnumProgressMode = .line
    
    /// 字体颜色
    public var textColor: UIColor = .black
    
    /// 字体样式
    public var textFont: UIFont = UIFont.systemFont(ofSize: 16)
    
    /// 背景颜色
    public var bgColor: UIColor = ZYUtil.colorWithHex("#FFF5E0")
    
    /// 背景圆角
    public var cornerRadius: CGFloat = 8
    
    private var zyHud: MBProgressHUD?
    
    /// 弹起进度条
    /// - Parameter text: text description
    public func show(_ text: String?) {
        if let keyWindown = zKeyWindow {
            if zyHud != nil {
                zyHud = nil
            }
            zyHud = MBProgressHUD.showAdded(to: keyWindown, animated: true)
            switch mode {
            case .line:
                zyHud?.mode = .determinateHorizontalBar
            case .circle:
                zyHud?.mode = .annularDeterminate
            case .indicator:
                zyHud?.mode = .indeterminate
            }
            if let t = text {
                zyHud?.label.text = t
                zyHud?.label.textColor = textColor
                zyHud?.label.font = textFont
            }
            zyHud?.bezelView.color = bgColor
            zyHud?.bezelView.layer.cornerRadius = cornerRadius
            zyHud?.isUserInteractionEnabled = false
        }
    }
    
    /// 隐藏进度条
    public func dismiss() {
        if zyHud != nil {
            zyHud?.hide(animated: true)
            zyHud = nil
        }
    }
    
}
